// pages/distribution/distribution.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: "https://head.image.jiajijie.com/dj/distribution",
    switchFlag: false,
    rulesFlag: false,
    friendsFlag: false,
    prediction: true,
    moveFlag: false,
    disFlag: false,
    oneFlag: true,
    twoFlag: true,
    moveList: [
      {
        "amount": 15,
        "wechat_name": "阿汤"
      },
      {
        "amount": 3.24,
        "wechat_name": "木子李"
      },
      {
        "amount": 7.94,
        "wechat_name": "十三爷"
      },
      {
        "amount": 7.57,
        "wechat_name": "姗姗"
      },
      {
        "amount": 6.27,
        "wechat_name": "蓝精灵"
      },
      {
        "amount": 9.96,
        "wechat_name": "小百叶"
      },
      {
        "amount": 6.22,
        "wechat_name": "Vincent"
      },
      {
        "amount": 3.62,
        "wechat_name": "王姑娘"
      },
      {
        "amount": 8.79,
        "wechat_name": "暖暖"
      },
      {
        "amount": 4.47,
        "wechat_name": "刚刚好"
      },
      {
        "amount": 9.43,
        "wechat_name": "安吉拉"
      },
      {
        "amount": 4.3,
        "wechat_name": "虎子"
      },
      {
        "amount": 5.68,
        "wechat_name": "大圣"
      },
      {
        "amount": 1.34,
        "wechat_name": "Aaton"
      },
      {
        "amount": 5.71,
        "wechat_name": "碧海蓝天"
      },
      {
        "amount": 8.91,
        "wechat_name": "李子"
      },
      {
        "amount": 1.98,
        "wechat_name": "王半仙儿"
      },
      {
        "amount": 5.38,
        "wechat_name": "修琅琳"
      },
      {
        "amount": 1.26,
        "wechat_name": "鲁冰没有花"
      },
      {
        "amount": 2.62,
        "wechat_name": "金大萌子"
      }
    ],
    a: 1

  },
  // 点击提取
  tixian() {
    if (this.data.account !== '0.00') {
      app.request('distribution/extract', {}, true).then(res => {

      })
      return
    }
    wx.showModal({
      content: '体现申请已提交，预计一个工作日到账，请注意查收。',
      showCancel: false
    })
    app.request("distribution", {}, true).then(res => {
      if (res.error_code == 0) {
        this.setData({
          account: res.data.account,//余额
          extract: res.data.extract,//体现的列表
        })
      }
    })
  },
  oneGrade() {
    this.setData({
      oneFlag: !this.data.oneFlag
    })
  },
  twoGrade() {
    this.setData({
      twoFlag: !this.data.twoFlag
    })
  },
  animation() {
    let a = 1
    this.setData({
      moveFlag: true,
      disFlag: false
    })
    setTimeout(() => {
      this.setData({
        moveFlag: false,
        disFlag: true
      })
    }, 2000)
    setInterval(() => {
      a++
      if (a >= 8) {
        a = 1
      }
      this.setData({
        a: a
      })
      this.setData({
        moveFlag: true,
        disFlag: false
      })
      setTimeout(() => {
        this.setData({
          moveFlag: false,
          disFlag: true
        })
      }, 2000)
    }, 4500)
  },

  stopMove() {

  },
  // 提取公积金
  pink() {
    wx.showLoading({
      title: '等待中',
      mask: true
    })
    wx.navigateTo({
      url: '../invite/invite?status=C',
    }, wx.hideLoading())
  },
  // 缴纳公积金
  gold() {
    wx.showLoading({
      title: '等待中',
      mask: true
    })
    wx.navigateTo({
      url: '../invite/invite?status=B',
    }, wx.hideLoading())
  },
  // 缴纳社保
  social() {
    wx.showLoading({
      title: '等待中',
      mask: true
    })
    wx.navigateTo({
      url: '../invite/invite?status=A',
    }, wx.hideLoading())
  },
  wintips() {
    wx.showLoading({
      title: '等待中',
      mask: true
    })
    wx.navigateTo({
      url: '../raiders/raiders',
    }, wx.hideLoading())
  },
  // 查看人数
  checkNum() {
    this.setData({
      friendsFlag: true
    })
  },
  // 查看规则
  rulesBtn() {
    this.setData({
      rulesFlag: true
    })
  },
  // 关闭规则
  closeRules() {
    this.setData({
      rulesFlag: false,
      friendsFlag: false
    })
  },
  // 预计收入
  expected() {
    this.setData({
      switchFlag: false
    })
  },
  // 已赚奖金
  earned() {
    this.setData({
      switchFlag: true
    })
  },

  login() {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      }, this.loadInfo)
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        app.globalData.userInfo = res.userInfo;
        this.setData({
          userInfo: res.userInfo,
        }, this.loadInfo)
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo
          }, this.loadInfo)
          let msg = {
            encrypteddata: res.encryptedData,
            iv: res.iv
          }
          // 暂时变更
          app.request("update_userinfo2", msg, true).then(res => {
            if (res.error_code == 0) {
              this.loadInfo();
            }
          })
        },
        fail: res => {
          console.log("拒绝")
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.animation();

    app.request("distribution", {}, true).then(res => {
      if (res.error_code == 0) {
        this.setData({
          complete: res.data.complete,//完成列表
          uncomplete: res.data.uncomplete,//未完成列表
          first_count: res.data.first_count,//一级好友
          second_count: res.data.second_count,//二级好友
          income: res.data.income,//总收入
          id: res.data.user_id,//总收入
          first_share: res.data.first_share,//一级好友列表
          second_share: res.data.second_share,//二级好友列表
          account: res.data.account,//余额
          extract: res.data.extract,//体现的列表
        })
      }
    })
    wx.getUserInfo({
      success: res => {
        this.setData({
          userInfo: res.userInfo
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // scene: "distribution_gongjijin_129457
    let that = this;
    // return {
    //   title: "我正在查询公积金，快来查询吧",
    //   path: `/pages/social/social?scene=distribution_shebao_${that.data.id}`,
    //   imageUrl: 'https://head.image.jiajijie.com/dj/distributionshare.png',
    //   success: function (res) {
    //   },
    //   fail: function (res) {
    //   }
    // }
    return {
      title: "社保/公积金代缴、公积金提取!",
      path: `/pages/home/home?scene=distribution_shebao_${that.data.id}`,
      imageUrl: 'https://head.image.jiajijie.com/dj/distributionshare.png',
      success: function (res) {
      },
      fail: function (res) {
        console.log(res)
      }
    }
  }
})